package the.workshop.milktea.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.workshop.milktea.infrastructure.mapper.DeviceMapper;
import the.workshop.milktea.infrastructure.model.Device;
import the.workshop.milktea.interfaces.resp.DeviceLockStatusResp;
import the.workshop.milktea.service.dto.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备 服务实现类
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Service
public class DeviceService extends AbstractSuperService<DeviceMapper, Device> {

    public boolean add(DeviceAddDTO dto) {
        Device entity = new Device();
        BeanUtils.copyProperties(dto, entity);

        entity.setCreatedId(dto.getOperatorId());
        entity.setCreatedName(dto.getOperatorName());
        entity.setCreatedTime(new Date());
        entity.setModifiedId(dto.getOperatorId());
        entity.setModifiedName(dto.getOperatorName());
        entity.setModifiedTime(new Date());
        this.baseMapper.insert(entity);
        return true;
    }

    public boolean modify(DeviceModifyDTO dto) {
        Device entity = new Device();
        BeanUtils.copyProperties(dto, entity);

        entity.setModifiedId(dto.getOperatorId());
        entity.setModifiedName(dto.getOperatorName());
        entity.setModifiedTime(new Date());
        return true;
    }

    public boolean modifyLockStatus(DeviceLockStatusModifyDTO dto) {
        baseMapper.update(null, new LambdaUpdateWrapper<Device>()
                .set(Device::getLockStatus, dto.getLockStatus())
                .set(Device::getModifiedTime, new Date())
                .set(Device::getModifiedId, dto.getOperatorId())
                .set(Device::getModifiedName, dto.getOperatorName())
                .eq(Device::getMac, dto.getMac())
        );
        return true;
    }

    public QueryResult<DevicePageResultDTO> page(DevicePageDTO condition) {
        if (condition.getPageNum() == null || condition.getPageSize() == null) {
            return null;
        }
        PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
        Page<DevicePageResultDTO> page = this.baseMapper.selectListPage(condition);
        return new QueryResult(page.getTotal(), page.getResult());
    }

    public DeviceGetDTO get(Integer id) {
        Device entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return null;
        }
        DeviceGetDTO dto = new DeviceGetDTO();
        BeanUtils.copyProperties(entity, dto);
        dto.setDeviceId(entity.getId());
        return dto;
    }

    public DeviceGetDTO getByMac(String mac) {
        List<Device> deviceList = baseMapper.selectList(new LambdaQueryWrapper<Device>()
                .eq(Device::getMac, mac)
        );
        if (CollectionUtils.isEmpty(deviceList)) {
            return null;
        }

        Device entity = deviceList.get(0);
        DeviceGetDTO dto = new DeviceGetDTO();
        BeanUtils.copyProperties(entity, dto);
        dto.setDeviceId(entity.getId());
        return dto;
    }

    public DeviceLockStatusResp getLockStatus(String mac) {
        List<Device> deviceList = baseMapper.selectList(new LambdaQueryWrapper<Device>()
                .eq(Device::getMac, mac)
        );
        return CollectionUtils.isEmpty(deviceList) ? null : convertOf(deviceList.get(0));
    }

    private DeviceLockStatusResp convertOf(Device source) {
        DeviceLockStatusResp target = new DeviceLockStatusResp();
        target.setMac(source.getMac());
        target.setLockStatus(source.getLockStatus());
        return target;
    }
}
